package com.tongchengsoft.tools;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String 工具类
 * 
 * @author PWWang
 * 
 */
public class StringTools {

	/**
	 * 获取34位UUID 没-且都为大写
	 * 
	 * @return
	 */
	public static String UUID() {

		String uuId = java.util.UUID.randomUUID().toString();

		try {

			String[] arr = uuId.split("-");
			StringBuffer sb = new StringBuffer();
			for (String string : arr) {
				sb.append(string);
			}
			uuId = sb.toString().toUpperCase();

		} catch (Exception e) {

			e.printStackTrace();
		}
		return uuId;
	}

	 /**
     * 获取一定长度的随机字符串
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
	
	/**
	 * 验证电话号码
	 * 
	 * @param phoneNumber
	 * @return
	 * @throws Exception
	 */
	public static boolean validatePhoneNumber(String phoneNumber)
			throws Exception {

		try {

			if (!phoneNumber.startsWith("1") || phoneNumber.length() != 11) {
				return false;
			}

			Pattern dx = null, lt = null, yd = null;
			Matcher d = null, l = null, y = null;
			dx = Pattern.compile("^1[3578][01379]\\d{8}$");
			lt = Pattern.compile("^1[34578][01256]\\d{8}$");
			yd = Pattern
					.compile("^(134[012345678]\\d{7}|1[34578][0123456789]\\d{8})$");
			String tel = phoneNumber;
			System.out.println("tel:" + tel);
			d = dx.matcher(tel);
			l = lt.matcher(tel);
			y = yd.matcher(tel);
			if (d.matches() || l.matches() || y.matches()) {
				return true;
			} else {
				return false;
			}

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	/**
	 * 字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {

		if (null == str || str.isEmpty() || "".equals(str.trim())
				|| 0 >= str.trim().length()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断字符串是否为正整数
	 * 
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static boolean isNumber(String str) throws Exception {

		try {

			for (int i = 0; i < str.length(); i++) {
				if (!Character.isDigit(str.charAt(i))) {
					return false;
				}
			}
			return true;
		} catch (Exception e) {

			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 获取最大优先级
	 * 
	 * @return
	 */
	public static Integer getWeightMax() {

		Double dob = Math.random();
		String str = dob.toString();
		str = str.substring(str.indexOf(".") + 1);
		if (str.indexOf("0") == 0) {
			int first = 0;
			while (true) {
				first = (int) (Math.random() * 10);
				if (first > 0) {
					break;
				}
			}
			str = str.replaceFirst("0", first + "");
		}
		str = str.substring(0, 9);

		return Integer.valueOf("1" + str);
	}

	public static String clearStr(String str) throws Exception {

		String[] clearArr = { "省", "市", "自治区", "特别行政区" };

		for (String string : clearArr) {

			int index = str.indexOf(string);
			if (index > 0) {
				str = str.substring(0, index);
				return str;
			}
		}

		return str;
	}

	/**
	 * 生成10位随机数字
	 * 
	 * @return
	 */
	public static long getSerialNumber() {

		Double dob = Math.random();
		String str = dob.toString();
		str = str.substring(str.indexOf(".") + 1);
		if (str.indexOf("0") == 0) {
			int first = 0;
			while (true) {
				first = (int) (Math.random() * 10);
				if (first > 0) {
					break;
				}
			}
			str = str.replaceFirst("0", first + "");
		}
		str = str.substring(0, 10);

		return Long.valueOf(str);
	}
	
	public static void main(String[] args) {

		try {
			System.out.println(StringTools.clearStr("内蒙古自治区"));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
